package net.lainiao.zhihu.backserver;

import net.lainiao.zhihu.dao.QutupageMapper;
import net.lainiao.zhihu.dao.QutupagegifMapper;
import net.lainiao.zhihu.model.Qutupage;
import net.lainiao.zhihu.model.Qutupagegif;
import org.springframework.context.ApplicationContext;
import net.lainiao.zhihu.reptitle.QutuCommon;

import java.util.List;

/**
 * Created by Administrator on 2016/1/19.
 */
public class QutuPageTask extends ZhihuTask implements Runnable {
    public QutuPageTask(ApplicationContext context) {
        super(context);
    }
    public void run() {
        QutupagegifMapper  qutupagegifMapper =context.getBean(QutupagegifMapper.class);
        QutupageMapper qutupageMapper=context.getBean(QutupageMapper.class);
        while(status==1){
            Qutupage qutu=qutupageMapper.getNext();
            if(qutu==null){
                break;
            }
            try {
                List<Qutupagegif> list= QutuCommon.getQutupagegifs(qutu.getUrl());
                for(Qutupagegif qutupagegif:list){
                    qutupagegif.setQutupageid(qutu.getId());
                    qutupagegifMapper.insert(qutupagegif);
                    Thread.sleep(100);
                }
            } catch (Exception e) {
                e.printStackTrace();
                qutu.setStatus(2);
                qutupageMapper.updateByPrimaryKey(qutu);
            }
        }
    }
}
